using System;
using System.Text;
using System.Data;
using System.Collections;
using System.Collections.Generic;

using Framework;

namespace SchemaMonitor
{
    //Table-Row Class (Customisable half)
    public partial class CPublishSection
    {
		#region Constants
		//Join Expressions
		private static string JOIN_PUBLISH = string.Concat(TABLE_NAME, " INNER JOIN ", CPublish.TABLE_NAME, " ON PubSecPublishId=PublishId"); //Nullable => Use LEFT OUTER JOIN
		#endregion

		#region Constructors (Public)
		//Default Connection String
		public CPublishSection() : base() {}
        public CPublishSection(int pubSecId) : base(pubSecId) {}
        
        //Alternative Connection String
        public CPublishSection(CDataSrc dataSrc) : base(dataSrc) {}
        
        public CPublishSection(CDataSrc dataSrc, int pubSecId) : base(dataSrc, pubSecId) {}
        
        //Transactional (shares an open connection)
        protected internal CPublishSection(CDataSrc dataSrc, int pubSecId, IDbTransaction txOrNull) : base(dataSrc, pubSecId, txOrNull) {}        
        #endregion

        #region Default Values
        protected override void InitValues_Custom()
        {
            //Custom default values (e.g. DateCreated column)
            //Member variables (e.g. for child collections)
        }
        #endregion
        
        #region Default Connection String
        protected override CDataSrc DefaultDataSrc()  { return CDataSrc.Default;  }
		#endregion

		#region Members
		//Foreign Keys
		[NonSerialized()]
		private CPublish _publish;
		//Child Collections  

		//Xml Data (as high-level objects)

		#endregion

		#region Properties - Relationships
		//Relationships - Foriegn Keys (e.g parent)
		public CPublish Publish
		{
			get
			{
				if (_publish == null)
				{
					lock (this)
					{
						if (_publish == null)
							_publish = new CPublish(this.PubSecPublishId);
					}
				}
				return _publish;
			}
			set
			{
				_publish = value;
				_pubSecPublishId = null != value ? value.PublishId : int.MinValue;
			}
		}


		//Relationships - Collections (e.g. children)

		#endregion

		#region Properties - Customisation
		//Derived/ReadOnly (e.g. xml classes, presentation logic)
		#endregion

		#region Save/Delete Overrides
		//Can Override base.Save/Delete (e.g. Cascade deletes, or insert related records)
		#endregion

		#region Custom Database Queries
		//For Stored Procs can use: MakeList (matching schema), or DataSrc.ExecuteDataset (reports etc)
		//For Dynamic sql, can use: SelectSum, SelectDistinct, SelectCount, SelectWhere (inherited methods)
		//                see also: SelectBy[FK], Search and Count (auto-generated sample queries)
		public CPublishSectionList SelectByPublishIds(List<int> publishIds) { return SelectWhere(new CCriteriaList("PubSecPublishId", ESign.IN, publishIds)); }

		#endregion

		#region Searching (Optional)
		//Dynamic search methods: (overload as required for common search patterns, cascade the BuildWhere overloads)
		//   Public  x5 - Simple, Paged, Transactional, Count, and Dataset
		//   Private x1 - BuildWhere
		//See also in-memory search options in list class, such as GetBy[FK] and Search

		//Simple
		public CPublishSectionList SelectSearch(string nameOrId /*, publishId, sectionId, postId*/)  {   return SelectWhere(BuildWhere(nameOrId /*, publishId, sectionId, postId*/));  } //, JOIN_OR_VIEW); }

        //Paged
        public CPublishSectionList SelectSearch(CPagingInfo pi, string nameOrId /*, int publishId, int sectionId, long postId */)
        {
            //pi.TableName = JOIN_OR_VIEW
            return SelectWhere(pi, BuildWhere(nameOrId /*, publishId, sectionId, postId*/));
        }

        //Transactional
        public CPublishSectionList SelectSearch(string nameOrId, /* int publishId, int sectionId, long postId, */ IDbTransaction tx)   {   return SelectWhere(BuildWhere(nameOrId /*, , publishId, sectionId, postId */), tx);   }   //, JOIN_OR_VIEW, tx); }

        //Dataset (e.g. ExportToCsv)
        public DataSet SelectSearch_Dataset(string nameOrId /*, int publishId, int sectionId, long postId*/)   {   return SelectWhere_Dataset(BuildWhere(nameOrId /*, , publishId, sectionId, postId */));   }   //, JOIN_OR_VIEW); }

        //Count
        public int SelectCount(string nameOrId /*, int publishId, int sectionId, long postId*/)   {   return SelectCount(BuildWhere(nameOrId /*, , publishId, sectionId, postId */));   }   //, JOIN_OR_VIEW); }

        //Filter Logic
        private CCriteriaList BuildWhere(string nameOrId /*, int publishId, int sectionId, long postId */)
        {
            CCriteriaList where = new CCriteriaList();  //Defaults to AND logic
            
            //Simple search box UI
            if (!string.IsNullOrEmpty(nameOrId)) 
            {
                //Interpret search string in various ways using OR sub-expression
                CCriteriaGroup orExpr = new CCriteriaGroup(EBoolOperator.Or);
                
                //Special case - search by PK
                /* 
                int id = 0;
                if (int.TryParse(nameOrId, out id))
                    orExpr.Add("PubSecId", id); 
                */
                
                //Search a range of string columns
                string wildCards = string.Concat("%", nameOrId, "%");

                //Conclude
                if (orExpr.Group.Count > 0)
                    where.Add(orExpr);
            }
            
			//Other search Colums (customise as required)
            //if (int.MinValue != publishId)   where.Add("PubSecPublishId", publishId);
            //if (int.MinValue != sectionId)   where.Add("PubSecSectionId", sectionId);
            //if (long.MinValue != postId)   where.Add("PubSecPostId", postId);

            return where;
        }
        #endregion


        #region Cloning
        public CPublishSection Clone(CDataSrc target, IDbTransaction txOrNull) //, int parentId)
        {
            //Shallow copy: Copies the immediate record, excluding autogenerated Pks
            CPublishSection copy = new CPublishSection(this, target);

            //Deep Copy - Child Entities: Cloned children must reference their cloned parent
            //copy.SampleParentId = parentId;

            copy.Save(txOrNull);

            //Deep Copy - Parent Entities: Cloned parents also clone their child collections
            //this.Children.Clone(target, txOrNull, copy.PubSecId);

            return copy;
        }
        #endregion

        #region ToXml
        protected override void ToXml_Custom(System.Xml.XmlWriter w)
        {
            //Store(w, "Example", this..Example)
        }
        #endregion
    }
}